//
//  AppDelegate.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/14.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "AppDelegate.h"


@interface AppDelegate () {
    
    UINavigationController *mainNav;
}

@end

@implementation AppDelegate


+ (AppDelegate *)shareAppDelegate
{
    return (AppDelegate *) [UIApplication sharedApplication].delegate;
}

- (UINavigationController *)shareNavigationController
{
    return mainNav;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    
    // 配置友盟SDK产品并并统一初始化
    // [UMConfigure setEncryptEnabled:YES]; // optional: 设置加密传输, 默认NO.
    // [UMConfigure setLogEnabled:YES]; // 开发调试时可在console查看友盟日志显示，发布产品必须移除。
    [UMConfigure initWithAppkey:@"5bc4838cf1f5568c7a000219" channel:@"App Store"];
    /* appkey: 开发者在友盟后台申请的应用获得（可在统计后台的 “统计分析->设置->应用信息” 页面查看）*/
    
    // 统计组件配置
    [MobClick setScenarioType:E_UM_NORMAL];
    // [MobClick setScenarioType:E_UM_GAME];  // optional: 游戏场景设置
    
    // Push组件基本功能配置
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
    //type是对推送的几个参数的选择，可以选择一个或者多个。默认是三个全部打开，即：声音，弹窗，角标等
    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert;
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            // 用户选择了接收Push消息
        }else{
            // 用户拒绝接收Push消息
        }
    }];
    //crashr统计
    [MobClick setCrashReportEnabled:YES];
    
    
    
    // 请参考「Share详细介绍-初始化第三方平台」
    // 分享组件配置，因为share模块配置可选三方平台较多，代码基本跟原版一样，也可下载demo查看
 //   [self configUSharePlatforms];   // required: setting platforms on demand

    
    [self getAppConfig];
    
    MainTabBarViewController *mainTabbar  = [[MainTabBarViewController alloc] init];
    mainNav = [[UINavigationController alloc] initWithRootViewController:mainTabbar];
    mainNav.navigationBarHidden = YES;
    //设置根控制器
    [self.window setRootViewController:mainNav];
    //窗口可视化
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void) getAppConfig {
    [HttpHelper request:@"app/config":@"GET" :nil
           successBlock:^(NSDictionary *responseDic) {
        
               //数据对齐
               ConfigModel *configModel = [ConfigModel mj_objectWithKeyValues:responseDic[@"data"]];
               //将请求的数据存入本地
               [[PINCache sharedCache] setObject:configModel forKey:@"config"];
               
               UserModel* userModel =[UserModel mj_objectWithKeyValues:responseDic[@"data"][@"user"]];
               [[PINCache sharedCache] setObject:userModel forKey:@"user"];
               //NSLog(@"%@", userModel.token);
               
              // NSLog(@"%@",configModel.order_list_tip);
               
    } failureBlock:^(NSError *error) {
        
        NSLog(@"请求出错了");
        
    }];

}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
